package org.sgroup.imenu.util;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.os.AsyncTask;
import android.util.Log;

public class DownloadTask extends AsyncTask<String, Void, Bitmap> {
	private Context context;
    private String namefile,path;
	public DownloadTask(Context _context) {
		this.context = _context;
	}

	protected Bitmap doInBackground(String... urls) {
		String urldisplay = urls[2];
		path=urls[0];
	//	namefile= urldisplay.substring(urldisplay.lastIndexOf("/") + 1);
        namefile=urls[1];
        if (namefile.lastIndexOf(".") ==-1) return null;
		Bitmap mIcon = null;
		try {
			InputStream in = new java.net.URL(urldisplay).openStream();
			BitmapFactory.Options options = new BitmapFactory.Options();
			// deh mas2ola enha trag3 1/2 el image
			options.inSampleSize = 1;
			options.inScaled = true;
			mIcon = BitmapFactory.decodeStream(in, null, options);
		} catch (Exception e) {
			Log.e("Error", e.getMessage());
			e.printStackTrace();
		}
		return mIcon;
	}

	protected void onPostExecute(Bitmap result) {
		if(result!=null) {
		new CData(context).saveToInternalSorage(result, namefile,path);}
		// Do your staff here to save image
	}

}
